package android.slc.extras.component.attchment.ui.adapter;

import android.content.res.ColorStateList;
import android.slc.appbase.R;
import android.slc.appbase.utils.GlideUtils;
import android.slc.extras.component.attchment.entity.Attachment;
import android.slc.extras.component.attchment.utils.AttachmentUtils;
import android.slc.attachment.bean.Progress;
import android.slc.commonlibrary.util.compat.SlcFileUtils;
import android.slc.mp.SlcMp;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.widget.ImageViewCompat;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dinuscxj.progressbar.CircleProgressBar;

import java.util.List;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/8/10 10:43
 */
public class AttachmentAdapter extends BaseQuickAdapter<Attachment, BaseViewHolder> {

    public AttachmentAdapter(@Nullable List<Attachment> data) {
        super(R.layout.app_item_attachment, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, Attachment attachment) {
        baseViewHolder.setText(R.id.tv_title, attachment.getAttachmentName());
        if (attachment.isLocalBody()) {
            String info = SlcFileUtils.byte2FitMemorySize(attachment.getLocalBody().getSize());
            info += "，";
            info += TimeUtils.millis2String(attachment.getLocalBody().getModified(), "yy-MM-dd:hh:mm");
            baseViewHolder.setText(R.id.tv_info, info);
        } else {
            baseViewHolder.setText(R.id.tv_info, StringUtils.getString(R.string.app_label_click_preview));
        }
        FrameLayout progressBarParent = baseViewHolder.getView(R.id.progress_bar_parent);
        Progress progress = attachment.getProgress();
        if (progress == null) {
            progressBarParent.setVisibility(View.GONE);
        } else {
            switch (progress.status) {
                case Progress.NONE:
                case Progress.ERROR:
                    progressBarParent.setVisibility(View.GONE);
                    break;
                case Progress.WAITING:
                case Progress.LOADING:
                case Progress.PAUSE:
                case Progress.FINISH:
                    progressBarParent.setVisibility(View.VISIBLE);
                    break;
            }
        }
        CircleProgressBar progressBar = baseViewHolder.getView(R.id.progress_bar);
        ImageButton actionBtn = baseViewHolder.getView(R.id.iv_action);
        if (progress == null) {
            actionBtn.setVisibility(attachment.isAllowEdit() ? View.VISIBLE : View.GONE);
            actionBtn.setImageResource(R.drawable.app_ic_attachment_delete);
            progressBar.setProgress(0);
        } else {
            /*if (progress.totalSize == -1) {
                progressBar.setProgress(50);
            } else if (progress.totalSize == 0) {
                progressBar.setProgress(100);
            } else {
                progressBar.setProgress(progress.fraction);
            }*/
            progressBar.setProgress(progress.fraction);
            switch (progress.status) {
                case Progress.ERROR:
                    actionBtn.setVisibility(View.VISIBLE);
                    actionBtn.setImageResource(R.drawable.app_ic_attchment_error);
                    break;
                case Progress.NONE:
                case Progress.WAITING:
                case Progress.LOADING:
                case Progress.PAUSE:
                case Progress.FINISH:
                    actionBtn.setVisibility(attachment.isAllowEdit() ? View.VISIBLE : View.GONE);
                    actionBtn.setImageResource(R.drawable.app_ic_attachment_delete);
                    break;
            }
        }
        ImageView iv_icon = baseViewHolder.getView(R.id.iv_icon);
        int mediaType = attachment.isLocalBody() ? attachment.getLocalBody().getMediaTypeTag()
                : AttachmentUtils.expandName2MediaType(attachment.getExpandName());
        if (mediaType == SlcMp.MEDIA_TYPE_PHOTO) {
            if (attachment.isLocalBody()) {
                Glide.with(iv_icon).load(attachment.getLocalPath()).apply(GlideUtils.getCenterCropOptions()).into(iv_icon);
            } else {
                Glide.with(iv_icon).load(attachment.getNetPath()).apply(GlideUtils.getCenterCropOptions()).into(iv_icon);
            }
        } else {
            ImageViewCompat.setImageTintList(iv_icon, ColorStateList.valueOf(ContextCompat.getColor(baseViewHolder.getView(R.id.ll_attachments_item_parent).getContext(), R.color.colorPrimary)));
            switch (mediaType) {
                case SlcMp.MEDIA_TYPE_AUDIO:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_audiotrack);
                    break;
                case SlcMp.MEDIA_TYPE_VIDEO:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_videocam);
                    break;
                case SlcMp.MEDIA_TYPE_FILE:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_unknown);
                    break;
                case AttachmentUtils.TYPE_WORD:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_word);
                    break;
                case AttachmentUtils.TYPE_EXCEL:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_excel);
                    break;
                case AttachmentUtils.TYPE_PPT:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_powerpoint);
                    break;
                case AttachmentUtils.TYPE_PDF:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_pdf);
                    break;
                case AttachmentUtils.TYPE_COMPRESSION:
                    iv_icon.setImageResource(R.drawable.slc_mp_ic_cs);
                    break;
            }
        }
    }
}
